package com.marakana.yamba1;

import java.io.IOException;

import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.chris.weibo.MainActivity;
import com.chris.weibo.R;
import com.marakana.yamba4.BaseActivity;
import com.weibo.sdk.android.WeiboException;
import com.weibo.sdk.android.api.StatusesAPI;
import com.weibo.sdk.android.net.RequestListener;

public class StatusActivity2 extends BaseActivity implements OnClickListener,
		TextWatcher {

	private static final String TAG = "StatusActivity";
	private EditText editText;
	private Button updateButton;
	private TextView textCount;
	private String mAccessToken = "";
	private String mContent = "";

	public static final String EXTRA_WEIBO_CONTENT = "com.weibo.android.content";
	public static final String EXTRA_PIC_URI = "com.weibo.android.pic.uri";
	public static final String EXTRA_ACCESS_TOKEN = "com.weibo.android.accesstoken";
	public static final String EXTRA_EXPIRES_IN = "com.weibo.android.token.expires";

	public static final int WEIBO_MAX_LENGTH = 140;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_status2);

		Intent in = this.getIntent();
		mAccessToken = in.getStringExtra(EXTRA_ACCESS_TOKEN);

		editText = (EditText) findViewById(R.id.editText);
		editText.addTextChangedListener(this);

		updateButton = (Button) findViewById(R.id.buttonUpdate);
		updateButton.setOnClickListener(this);

		textCount = (TextView) findViewById(R.id.textCount);
		textCount.setText("140");
		textCount.setTextColor(Color.GREEN);

	}

	@Override
	public void onClick(View v) {
		new PostToSina().execute(editText.getText().toString());
		Log.d(TAG, "OnClick");
	}

	@Override
	public void afterTextChanged(Editable s) {
		int count = 140 - s.length();
		textCount.setText(Integer.toString(count));
		textCount.setTextColor(Color.GREEN);
		if (count < 10)
			textCount.setTextColor(Color.YELLOW);
		if (count < 0)
			textCount.setTextColor(Color.RED);
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
	}

	class PostToSina extends AsyncTask<String, Integer, String> implements
			RequestListener {

		@Override
		protected String doInBackground(String... params) {
			StatusActivity2 statusActivity2 = StatusActivity2.this;

			StatusesAPI api = new StatusesAPI(MainActivity.accessToken);
			if (!TextUtils.isEmpty(mAccessToken)) {
				statusActivity2.mContent = params[0];
				if (TextUtils.isEmpty(mContent)) {
					Toast.makeText(StatusActivity2.this, "请输入内容!",
							Toast.LENGTH_LONG).show();
				} else {
					api.update( statusActivity2.mContent, "90.0", "90.0", this);
				}
			} else {
				Toast.makeText(
						StatusActivity2.this,
						statusActivity2
								.getString(R.string.weibosdk_please_login),
						Toast.LENGTH_LONG).show();
			}

			return null;
		}

		@Override
		public void onComplete(String response) {
			runOnUiThread(new Runnable() {

				@Override
				public void run() {
					Toast.makeText(StatusActivity2.this,
							R.string.weibosdk_send_sucess, Toast.LENGTH_LONG)
							.show();
				}
			});
			StatusActivity2.this.finish();
		}

		@Override
		public void onIOException(IOException e) {

		}

		@Override
		public void onError(final WeiboException e) {
			System.out.println(e);
			runOnUiThread(new Runnable() {

				@Override
				public void run() {
					Toast.makeText(
							StatusActivity2.this,
							String.format(
									StatusActivity2.this
											.getString(R.string.weibosdk_send_failed)
											+ ":%s", e.getMessage()),
							Toast.LENGTH_LONG).show();
				}
			});

		}

	}

}
